/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/08/22
 */


#include "ConfigManager.h"
#include "LogManager.h"
#include <QCoreApplication>
#include <QStringList>

template<> ConfigManager* Singleton < ConfigManager >::ms_Singleton = 0;

void ConfigManager::init () {

	if ( !mShutdown )
		return;

	LogManager::message ( "ConfigManager::Starting Up" );

	mSettings = new QSettings ( QSettings::IniFormat, QSettings::UserScope,
			QCoreApplication::organizationName (),
			QCoreApplication::applicationName (), this );

	mShutdown = false;
}

void ConfigManager::shutdown () {

	if ( mShutdown )
		return;

	LogManager::message ( "ConfigManager::Shutting down" );

	mShutdown = true;
}

int ConfigManager::getInt ( const QString& rKey, int rDefaultValue,
		const QString& rGroup ) {

	if ( mShutdown )
		return 0;

	mSettings->beginGroup ( rGroup );

	if ( !mSettings->contains ( rKey ) ) {
		mSettings->endGroup ();
		setInt ( rKey, rDefaultValue, rGroup );
		return rDefaultValue;
	}

	int value = getValue ( rKey, QVariant ( rDefaultValue ) ).toInt ();
	mSettings->endGroup ();
	return value;
}

void ConfigManager::setInt ( const QString& rKey, int rValue,
		const QString& rGroup ) {

	if ( mShutdown )
		return;

	mSettings->beginGroup ( rGroup );
	setValue ( rKey, QVariant ( rValue ) );
	mSettings->endGroup ();

}

bool ConfigManager::getBool ( const QString& rKey, bool rDefaultValue,
		const QString& rGroup ) {

	if ( mShutdown )
		return false;

	mSettings->beginGroup ( rGroup );

	if ( !mSettings->contains ( rKey ) ) {
		mSettings->endGroup ();
		setBool ( rKey, rDefaultValue, rGroup );
		return rDefaultValue;
	}

	bool value = getValue ( rKey, QVariant ( rDefaultValue ) ).toBool ();
	mSettings->endGroup ();
	return value;

}

void ConfigManager::setBool ( const QString& rKey, bool rValue,
		const QString& rGroup ) {

	if ( mShutdown )
		return;

	mSettings->beginGroup ( rGroup );
	setValue ( rKey, QVariant ( rValue ) );
	mSettings->endGroup ();

}

float ConfigManager::getFloat ( const QString& rKey, float rDefaultValue,
		const QString& rGroup ) {

	if ( mShutdown )
		return 0.0f;

	mSettings->beginGroup ( rGroup );

	if ( !mSettings->contains ( rKey ) ) {
		mSettings->endGroup ();
		setFloat ( rKey, rDefaultValue, rGroup );
		return rDefaultValue;
	}

	float value = getValue ( rKey, QVariant ( rDefaultValue ) ).toFloat ();
	mSettings->endGroup ();
	return value;
}

void ConfigManager::setFloat ( const QString& rKey, float rValue,
		const QString& rGroup ) {

	if ( mShutdown )
		return;

	mSettings->beginGroup ( rGroup );
	setValue ( rKey, QVariant ( rValue ) );
	mSettings->endGroup ();

}

QString ConfigManager::getString ( const QString& rKey,
		const QString& rDefaultValue, const QString& rGroup ) {

	if ( mShutdown )
		return QString::null;

	mSettings->beginGroup ( rGroup );

	if ( !mSettings->contains ( rKey ) ) {
		mSettings->endGroup ();
		setString ( rKey, rDefaultValue, rGroup );
		return rDefaultValue;
	}

	QString value = getValue ( rKey, QVariant ( rDefaultValue ) ).toString ();
	mSettings->endGroup ();
	return value;

}

void ConfigManager::setString ( const QString& rKey, const QString& rValue,
		const QString& rGroup ) {

	if ( mShutdown )
		return;

	mSettings->beginGroup ( rGroup );
	setValue ( rKey, QVariant ( rValue ) );
	mSettings->endGroup ();
}

QVariant ConfigManager::getValue ( const QString &rKey,
		const QVariant& rDefaultValue ) {

	if ( mShutdown )
		return QVariant ();

	return mSettings->value ( rKey, rDefaultValue );
}

QVariant ConfigManager::getValue ( const QString& rKey, const QString& rGroup,
		const QVariant& rDefaultValue ) {

	if ( mShutdown )
		return QVariant ();

	mSettings->beginGroup ( rGroup );
	QVariant value = mSettings->value ( rKey, rDefaultValue );
	mSettings->endGroup ();

	return value;
}

void ConfigManager::setValue ( const QString & rKey, const QVariant & rValue ) {

	if ( mShutdown )
		return;

	mSettings->setValue ( rKey, rValue );
}

void ConfigManager::setValue ( const QString& rKey, const QVariant& rValue,
		const QString& rGroup ) {

	mSettings->beginGroup ( rGroup );
	mSettings->setValue ( rKey, rValue );
	mSettings->endGroup ();
}

bool ConfigManager::valueExist ( const QString& rKey, const QString& rGroup ) {

	mSettings->beginGroup ( rGroup );
	QStringList keys = mSettings->childKeys ();
	bool result = keys.contains ( rKey );
	mSettings->endGroup ();

	return result;
}
